package vn.vietel.pms.business;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import org.springframework.transaction.annotation.Transactional;

import vn.vietel.pms.entity.VTeam;
import vn.vietel.pms.service.TeamService;

public class TeamServiceImpl implements TeamService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void create(VTeam item) {
		if (item != null) {
			entityManager.persist(item);
		}
	}

	@Transactional
	public VTeam find(Long id) {
		return entityManager.find(VTeam.class, id);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Transactional
	public void delete(VTeam item) {
		entityManager.remove(item);

	}

	@Transactional
	public VTeam find(String name) {
		try {
			return (VTeam) entityManager
					.createQuery(
							"select g from VTeam g where g.teamname=:name")
					.setParameter("name", name).getSingleResult();
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Transactional
	public void update(VTeam item) {
		entityManager.merge(item);

	}

	@SuppressWarnings("unchecked")
	public List<VTeam> getAll() {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VTeam g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}
	

}
